package com.github.lee.unsplash.utils.dialog

import android.content.Context
import android.graphics.Color
import androidx.annotation.Nullable
import com.github.lee.unsplash.R
import com.github.lee.unsplash.databinding.DialogCenterTipsBinding
import com.github.lee.unsplash.ui.auth.WebAuthActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.XPopupCallback

class CenterTipsDialog(context: Context) : CenterPopupView(context) {

    private val pageVB by lazy { DialogCenterTipsBinding.bind(popupImplView) }

    companion object {
        fun show(
            context: Context, canDismiss: Boolean,
            @Nullable title: String?, content: String,
            positiveButton: DialogButton,
            @Nullable negativeButton: DialogButton? = null
        ): BasePopupView {
            val dialog = CenterTipsDialog(context)
            return XPopup.Builder(context)
                .dismissOnBackPressed(canDismiss)
                .dismissOnTouchOutside(canDismiss)
                .setPopupCallback(object : XPopupCallback {
                    override fun onCreated(popupView: BasePopupView?) {

                    }

                    override fun beforeShow(popupView: BasePopupView?) {
                        dialog.onSetViewData(title, content, negativeButton, positiveButton)
                    }

                    override fun onShow(popupView: BasePopupView?) {
                    }

                    override fun onDismiss(popupView: BasePopupView?) {

                    }

                    override fun beforeDismiss(popupView: BasePopupView?) {

                    }

                    override fun onBackPressed(popupView: BasePopupView?): Boolean = !canDismiss

                    override fun onKeyBoardStateChanged(popupView: BasePopupView?, height: Int) {

                    }

                    override fun onDrag(
                        popupView: BasePopupView?,
                        value: Int,
                        percent: Float,
                        upOrLeft: Boolean
                    ) {

                    }
                })
                .asCustom(dialog)
                .show()
        }


        fun showLoginTipDialog(context: Context) {
            CenterTipsDialog.show(
                context, true, context.getString(R.string.txt_tips),
                context.getString(R.string.txt_login_tips),
                DialogButton(
                    context.getString(
                        R.string.xpopup_ok
                    ),
                    textColor = Color.WHITE,
                    background = Color.parseColor("#FF5075FC"),
                    borderColor = Color.parseColor("#FF5075FC"),
                    clickCall = {
                        it.dismissWith {
                            WebAuthActivity.start(context)
                        }
                    }
                ),
                DialogButton(
                    context.getString(
                        R.string.xpopup_cancel
                    ),
                    textColor = Color.parseColor("#FF5075FC"),
                    background = Color.WHITE,
                    borderColor = Color.parseColor("#FF5075FC"),
                    clickCall = {
                        it.dismiss()
                    }
                )
            )
        }
    }


    //===Desc:================================================================================

    override fun getImplLayoutId(): Int = R.layout.dialog_center_tips

    override fun onCreate() {
        super.onCreate()
    }


    private fun onSetViewData(
        title: String?,
        content: String,
        leftBtn: DialogButton?,
        rightBtn: DialogButton
    ) {
        pageVB.title = title
        pageVB.content = content
        pageVB.leftBtn = leftBtn
        if (null != leftBtn) {
            pageVB.leftClick = {
                leftBtn.clickCall.invoke(this)
            }
        }
        pageVB.rightBtn = rightBtn
        pageVB.rightClick = {
            rightBtn.clickCall.invoke(this)
        }


    }

}